<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Receives data from a socket whether or not it is connection-oriented</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.socket-recv.html">socket_recv</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.socket-recvmsg.html">socket_recvmsg</a></div>
 <div class="up"><a href="ref.sockets.html">Socket 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.socket-recvfrom" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_recvfrom</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5)</p><p class="refpurpose"><span class="refname">socket_recvfrom</span> &mdash; <span class="dc-title">Receives data from a socket whether or not it is connection-oriented</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-recvfrom-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>socket_recvfrom</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$socket</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$buf</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$len</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$name</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$port</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   The <span class="function"><strong>socket_recvfrom()</strong></span> function receives
   <em><code class="parameter">len</code></em> bytes of data in <em><code class="parameter">buf</code></em> from
   <em><code class="parameter">name</code></em> on port <em><code class="parameter">port</code></em> (if the
   socket is not of type <strong><code>AF_UNIX</code></strong>) using
   <em><code class="parameter">socket</code></em>. <span class="function"><strong>socket_recvfrom()</strong></span> can be
   used to gather data from both connected and unconnected sockets.
   Additionally, one or more flags can be specified to modify the behaviour of
   the function.
  </p>
  <p class="para">
   The <em><code class="parameter">name</code></em> and <em><code class="parameter">port</code></em> must be
   passed by reference. If the socket is not connection-oriented,
   <em><code class="parameter">name</code></em> will be set to the internet protocol address of
   the remote host or the path to the UNIX socket. If the socket is
   connection-oriented, <em><code class="parameter">name</code></em> is <strong><code>NULL</code></strong>. Additionally,
   the <em><code class="parameter">port</code></em> will contain the port of the remote host in
   the case of an unconnected <strong><code>AF_INET</code></strong> or
   <strong><code>AF_INET6</code></strong> socket. 
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-recvfrom-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">socket</code></em></dt>

     <dd>

      <p class="para">
       The <em><code class="parameter">socket</code></em> must be a socket resource previously
       created by socket_create().
      </p>
     </dd>

    

    
     <dt>
<em><code class="parameter">buf</code></em></dt>

     <dd>

      <p class="para">
       The data received will be fetched to the variable specified with
       <em><code class="parameter">buf</code></em>.
      </p>
     </dd>

    

    
     <dt>
<em><code class="parameter">len</code></em></dt>

     <dd>

      <p class="para">
       Up to <em><code class="parameter">len</code></em> bytes will be fetched from remote host.
      </p>
     </dd>

    

    
     <dt>
<em><code class="parameter">flags</code></em></dt>

     <dd>

      <p class="para">
       The value of <em><code class="parameter">flags</code></em> can be any combination of 
       the following flags, joined with the binary OR (<em>|</em>)
       operator.
      </p>
      
      <table class="doctable table">
       <caption><strong>Possible values for <em><code class="parameter">flags</code></em></strong></caption>
       
        <thead>
         <tr>
          <th>Flag</th>
          <th>Description</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td><strong><code>MSG_OOB</code></strong></td>
          <td>
           Process out-of-band data.
          </td>
         </tr>

         <tr>
          <td><strong><code>MSG_PEEK</code></strong></td>
          <td>
           Receive data from the beginning of the receive queue without
           removing it from the queue.
          </td>
         </tr>

         <tr>
          <td><strong><code>MSG_WAITALL</code></strong></td>
          <td>
           Block until at least <em><code class="parameter">len</code></em> are received.
           However, if a signal is caught or the remote host disconnects, the
           function may return less data.
          </td>
         </tr>

         <tr>
          <td><strong><code>MSG_DONTWAIT</code></strong></td>
          <td>
           With this flag set, the function returns even if it would normally
           have blocked.
          </td>
         </tr>

        </tbody>
       
      </table>

     </dd>

    

    
     <dt>
<em><code class="parameter">name</code></em></dt>

     <dd>

      <p class="para">
       If the socket is of the type <strong><code>AF_UNIX</code></strong> type,
       <em><code class="parameter">name</code></em> is the path to the file. Else, for
       unconnected sockets, <em><code class="parameter">name</code></em> is the IP address of,
       the remote host, or <strong><code>NULL</code></strong> if the socket is connection-oriented.
      </p>
     </dd>

    

    
     <dt>
<em><code class="parameter">port</code></em></dt>

     <dd>

      <p class="para">
       This argument only applies to <strong><code>AF_INET</code></strong> and
       <strong><code>AF_INET6</code></strong> sockets, and specifies the remote port
       from which the data is received. If the socket is connection-oriented,
       <em><code class="parameter">port</code></em> will be <strong><code>NULL</code></strong>.
      </p>
     </dd>

    
    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.socket-recvfrom-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>socket_recvfrom()</strong></span> returns the number of bytes received,
   or <strong><code>FALSE</code></strong> if there was an error. The actual error code can be retrieved by 
   calling <span class="function"><a href="function.socket-last-error.html" class="function">socket_last_error()</a></span>. This error code may be
   passed to <span class="function"><a href="function.socket-strerror.html" class="function">socket_strerror()</a></span> to get a textual explanation
   of the error.
  </p>
 </div>

                     
 <div class="refsect1 examples" id="refsect1-function.socket-recvfrom-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4498">
    <p><strong>Example #1 <span class="function"><strong>socket_recvfrom()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />error_reporting</span><span style="color: #007700">(</span><span style="color: #0000BB">E_ALL&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">E_STRICT</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$socket&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_DGRAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_UDP</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">socket_bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1223</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$from&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">socket_recvfrom</span><span style="color: #007700">(</span><span style="color: #0000BB">$socket</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$buf</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">12</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$from</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$port</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Received&nbsp;</span><span style="color: #0000BB">$buf</span><span style="color: #DD0000">&nbsp;from&nbsp;remote&nbsp;address&nbsp;</span><span style="color: #0000BB">$from</span><span style="color: #DD0000">&nbsp;and&nbsp;remote&nbsp;port&nbsp;</span><span style="color: #0000BB">$port</span><span style="color: #DD0000">"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     This example will initiate a UDP socket on port 1223 of 127.0.0.1
     and print at most 12 characters received from a remote host.
    </p></div>
   </div>
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.socket-recvfrom-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.3.0</td>
       <td>
        <span class="function"><strong>socket_recvfrom()</strong></span> is now binary safe.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.socket-recvfrom-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.socket-recv.html" class="function" rel="rdfs-seeAlso">socket_recv()</a> - Receives data from a connected socket</span></li>
    <li class="member"><span class="function"><a href="function.socket-send.html" class="function" rel="rdfs-seeAlso">socket_send()</a> - Sends data to a connected socket</span></li>
    <li class="member"><span class="function"><a href="function.socket-sendto.html" class="function" rel="rdfs-seeAlso">socket_sendto()</a> - Sends a message to a socket, whether it is connected or not</span></li>
    <li class="member"><span class="function"><a href="function.socket-create.html" class="function" rel="rdfs-seeAlso">socket_create()</a> - 创建一个套接字（通讯节点）</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.socket-recv.html">socket_recv</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.socket-recvmsg.html">socket_recvmsg</a></div>
 <div class="up"><a href="ref.sockets.html">Socket 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
